from flask import Blueprint, request

from day12.user_server import u_list

users_route=Blueprint("users", __name__ ,url_prefix="/u")


@users_route.route("/get",methods=["post"])
def get():
    return u_list

@users_route.route("/add",methods=["post"])
def add():
    data=request.form
    id=int(data.get("id"))
    name=data.get("name")
    age=int(data.get("age"))
    for i in u_list:
        id_user=i[0]
        name_user=i[1]
        age_user=i[2]
        if id_user==id or name_user==name:
            return f"已存在id或name"
    else:
        u_list.append([id, name, age])
        return u_list

@users_route.route("/inquire/<int:id>'",methods=["get"])
def inquire(id):
    for i in u_list:
        id_user=i[0]
        if id_user==id:
            return [i[0], i[1], i[2]]
        else:
            return f"查询失败,找不到"

@users_route.route("/delete/<int:id>",methods=["delete"])
def delete(id):
    for i in u_list:
        id_user = i[0]
        if id_user == id:
            u_list.remove({i[0],i[1],i[2]})
            return u_list
    else:
        return f"删除失败,找不到"

@users_route.route("update/<int:id>",methods=["put"])
def update():
    data = request.form
    id = int(data.get("id"))
    name = data.get("name")
    age = int(data.get("age"))
    for i in u_list:
        id_user = i[0]
        if id_user == id:
            u_list.remove({i[0], i[1], i[2]})
            u_list.append([id, name, age])
            return u_list
    else:
        return f"更新失败,找不到对应id"